package com.spring.demo3.eg2;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.LazyLoader;

/**
 * @author vhans
 */
public class BlogLazyLoader implements LazyLoader {
    @Override
    public Object loadObject() throws Exception {
        // 模拟从数据库中获取博客内容
        System.out.println("准备从数据库中获取博客内容...");
        BlogContent blogContent = new BlogContent();
        blogContent.setContent("学习spring aop的知识，成为spring的高手");
        return blogContent;
    }

    public static  <T> T createProxy(T target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new BlogLazyLoader());
        return (T) enhancer.create();
    }
}
